package com.yulebaby.callsenter.ui.view;

import android.graphics.Color;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.bean.SaleDailyBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2017/1/23
 */
public class StatisticalParallelColumn {


    public final static String[] stepTarget = new String[]{"步骤一", "步骤二", "步骤三", "步骤四", "步骤五", "步骤六", "步骤七"};
    private BarChart mChart;

    // 设置图表样式
    public StatisticalParallelColumn(BarChart chart) {
        this.mChart = chart;
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.animateXY(1000, 1000);
        mChart.setNoDataText("");
        mChart.setMaxVisibleValueCount(60);
        mChart.setDescription("");


        mChart.setDrawGridBackground(false);

        // 设置是否可以触摸
        mChart.setTouchEnabled(false);
        // 是否可以拖拽
        mChart.setDragEnabled(false);
        // 是否可以缩放
        mChart.setScaleEnabled(false);
        // 集双指缩放
        mChart.setPinchZoom(false);
        // 隐藏右边的坐标轴
        mChart.getAxisRight().setEnabled(false);
        // 隐藏左边的左边轴
        mChart.getAxisLeft().setEnabled(false);


        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setSpaceBetweenLabels(2);


        Legend l = mChart.getLegend();
        l.setEnabled(false);

        mChart.invalidate();
    }

    public void setData(List<SaleDailyBean> result) {

        ArrayList<BarEntry> valueNow = new ArrayList<>();
        ArrayList<BarEntry> valuePre = new ArrayList<>();

        for (int i = 0; i < 7; ++i) {
            for (int j = 0; j < result.size(); j++) {
                SaleDailyBean bean = result.get(j);
                if ((bean.getStep() - 1) == i) {
                    valuePre.add(new BarEntry((float) bean.getCount_actual_tran(), i));
                    valueNow.add(new BarEntry((float) bean.getCount_tran_targetnumint(), i));
                } else if (j == result.size()) {
                    valuePre.add(new BarEntry(0, i));
                    valueNow.add(new BarEntry(0, i));
                }
            }
        }

        BarDataSet set1;
        BarDataSet set2;

        set1 = new BarDataSet(valuePre, "实际转化");
        set1.setColor(Color.parseColor("#1798FB"));
        set1.setBarShadowColor(Color.parseColor("#01000000"));

        set2 = new BarDataSet(valueNow, "");
        set2.setColor(Color.parseColor("#3EC46D"));
        set2.setBarShadowColor(Color.parseColor("#01000000"));

        ArrayList<BarDataSet> list = new ArrayList<>();
        list.add(set1);
        list.add(set2);

        BarData data = new BarData(Arrays.asList(stepTarget), list);

        for (int i = 0; i < valuePre.size(); i++) {
            BarEntry barPre = valuePre.get(i);
            BarEntry barNow = valueNow.get(i);
            if (barNow.getVal() >= barPre.getVal()) {
                if (i == 0) {
                    BarDataSet index = data.getDataSetByIndex(1);
                    index.setColor(Color.parseColor("#3EC46D"));
                } else {
                    BarDataSet index = data.getDataSetByIndex(1);
                    index.addColor(Color.parseColor("#3EC46D"));
                }
            } else {
                if (i == 0) {
                    BarDataSet index = data.getDataSetByIndex(1);
                    index.setColor(Color.parseColor("#FF9800"));
                } else {
                    BarDataSet index = data.getDataSetByIndex(1);
                    index.addColor(Color.parseColor("#FF9800"));
                }

            }
        }

        data.setValueFormatter(value -> (int) value == 0 ? "" : (int) value + "");

        data.setValueTextSize(10f);

        mChart.setData(data);

        mChart.invalidate();
    }
}
